home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / gigo0714.zip / COMPILE.CPP < prev    next >
Text File  |  1994-08-29  |  7KB  |  313 lines

  1. #include <conio.h>
  2. #include <ctype.h>
  3. #include <direct.h>
  4. #include <dos.h>
  5. #include <fcntl.h>
  6. #include <io.h>
  7. #include <share.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys\stat.h>
  13. #include <ctype.h>
  14.  
  15. #define addbackslash(x)  if (x[0]) if (x[strlen(x)-1]!='\\') strcat(x,"\\")
  16.  
  17.  
  18. #ifdef __OS2__
  19.  #define DW unsigned short
  20. #else
  21.  #define DW unsigned int
  22. #endif
  23.  
  24.   #define INBUFSIZE 32000
  25.   #define OUTBUFSIZE 8192
  26.  
  27.   #define bumpby 35
  28.  
  29. // #define messy    This define shows us what it's compiling
  30.  
  31. char defaultdir[256] = ".\\";
  32. char defaultnodelist[256] = "NODELIST";
  33. char workbuf[256];
  34. int append=0;
  35.  
  36. int inhandle=0;
  37. char *inbuf=NULL;
  38. int insize=0,inptr=0,inread=0;
  39.  
  40. FILE *outfile=NULL;
  41.    char namebuf[256];
  42.  
  43.   static char *wb1=&workbuf[1];
  44.   static char *wb4=&workbuf[4];
  45.   static char *wb5=&workbuf[5];
  46.   static char *wb7=&workbuf[7];
  47.   static char *wb8=&workbuf[8];
  48.  
  49.   static DW uZONE=65535u;
  50.   static DW uHOST=65534u;
  51.  
  52.  
  53.  
  54. void myatoi(char *p)
  55. {
  56.  DW u=0;
  57.  while ((*p)!=',')
  58.   u=(DW) (u*10-'0'+(*p++));
  59.  if ((u&uHOST)==uHOST) // also covers uZONE
  60.   {
  61.   cprintf("Skipping a node using 65534 or 65535 as an address.\r\n");
  62.   u=0;
  63.   }
  64.  fwrite(&u,2,1,outfile);
  65. }
  66.  
  67. /*
  68. char nextchar(void)
  69. {
  70.  if (inptr>=inread)
  71.    {
  72.     inptr=0;
  73.     inbuf[0]=0;
  74.     inread=read(inhandle,inbuf,insize);
  75.     if (!inread) return 0;
  76.    }
  77.  return(inbuf[inptr++]);
  78. }
  79. */
  80. #define prepnextchar(); if (inptr>=inread) {inptr=0;inbuf[0]=0;inread=read(inhandle,inbuf,insize);}
  81. #define nextchar() inbuf[inptr++]
  82.  
  83. void nextline(char *workbuf)
  84. {
  85.  register char ch;
  86. runagain:
  87.  *workbuf=0;
  88.  prepnextchar();
  89.  *workbuf=nextchar();
  90.  if (!*workbuf) return;
  91.  if (*workbuf==13) return;
  92.  if (*workbuf==27) return;
  93.  if (*workbuf==10) return;
  94.  if (*workbuf==';')
  95.    {
  96.     do
  97.      {prepnextchar();ch=nextchar();}
  98.     while ((ch!=0) && (ch!='\n'));
  99.     goto runagain;
  100.    }
  101.  if (*workbuf==26) {*workbuf=0;return;}
  102.  do  //first string + ,
  103.    {workbuf++;
  104.     prepnextchar();
  105.     *workbuf=ch=nextchar();}
  106.  while (*workbuf!=',');
  107.  
  108.  do  // second char + ,
  109.    { workbuf++;
  110.      prepnextchar();
  111.      *workbuf=nextchar();}
  112.  while (*workbuf!=',');
  113.  
  114.  inptr+=bumpby;
  115.  if (inptr>=inread) inptr=inread;
  116.  
  117.  do
  118.    {prepnextchar();
  119.     ch=nextchar();}
  120.     while ((ch!=0) && (ch!='\n'));
  121.  return;
  122. }
  123. int why=0;
  124. void parselines(void)
  125. {
  126. //  register DW value;
  127.  
  128. /*
  129.     Hub,        4
  130.     Pvt,        4
  131.     Zone,       5
  132.     Host,       5
  133.     Region,     7
  134.     */
  135.  
  136. newline:
  137.   nextline(workbuf);
  138.   switch (*workbuf)
  139.    {
  140.     case 0   : return;
  141.  
  142.     case ',' : myatoi(wb1);
  143.                #ifdef messy
  144.                  gotoxy(12,why);cprintf("%4u",atoi(wb1));
  145.                #endif
  146.                goto newline;
  147.  
  148.     case 'H' : if (workbuf[1]=='u') goto Hub; // we only want HOST here
  149.                if (workbuf[2]=='l') goto priv; // DOWN? make it private
  150.                #ifdef messy
  151.                  gotoxy(7,why);cprintf("%4u",atoi(wb5));
  152.                #endif
  153.                fwrite(&uHOST,2,1,outfile);
  154.                myatoi(wb5);goto newline;
  155.  
  156.     Hub      : myatoi(wb4);goto newline;
  157.  
  158. //    case 'p' :
  159.     priv     :
  160.     case 'D' :  // down
  161.     case 'P' : myatoi(wb4); goto newline;
  162.  
  163.     case 'R' : fwrite(&uHOST,2,1,outfile);
  164.                #ifdef messy
  165.                  gotoxy(7,why);cprintf("%4u",atoi(wb7));
  166.                #endif
  167.                myatoi(wb7);goto newline;
  168.  
  169.     case 'Z' : fwrite(&uZONE,2,1,outfile);
  170.                #ifdef messy
  171.                  gotoxy(1,why);cprintf("%4u",atoi(wb5));
  172.                #else
  173.                 cprintf("\r\nZone %u",atoi(wb5));
  174.                #endif
  175.                myatoi(wb5);goto newline;
  176.  
  177.    }
  178.   goto newline;
  179. }
  180. void parsenodelist(void)
  181. {  int ibsize;
  182.   inhandle=sopen(defaultnodelist, O_BINARY|O_RDONLY, SH_DENYNO, S_IREAD);
  183.   if (inhandle<1)
  184.    {
  185.     cprintf("Could not open nodelist file %s.\r\n",defaultnodelist);
  186.     exit(1);
  187.    }
  188.  
  189.  
  190.   if (append)
  191.     outfile= fopen("NODELIST.GIG", "ab");
  192.   else
  193.     outfile= fopen("NODELIST.GIG", "wb");
  194.   if (!outfile)
  195.    {
  196.     cprintf("Could not open output index file %s.\r\n",workbuf);
  197.     exit(1);
  198.    }
  199.  
  200.   if (setvbuf(outfile, NULL, _IOFBF, OUTBUFSIZE) != 0)
  201.      cprintf("Failed to set up output buffer\r\n");
  202.  
  203.   ibsize=INBUFSIZE;
  204.  loop:
  205.   if (ibsize<256)
  206.    {
  207.     cprintf("Failed to set up input buffer.\r\n");
  208.     fclose(outfile);
  209.     exit(1);
  210.    }
  211.   inbuf=(char *) calloc(1,ibsize);
  212.   if (!inbuf) {ibsize-=128;goto loop;}
  213.   insize=ibsize;inptr=0,inread=0;
  214. #ifdef messy
  215.   why = wherey();
  216. #endif
  217.   parselines();
  218.  
  219.   close(inhandle);
  220.   fclose(outfile);
  221. }
  222.  
  223. char *getnodelist(char *dir, char *name)
  224. char *p; 
  225.  
  226. DIR *dirp;
  227. char buf[255];
  228. unsigned long d_date,d_time;
  229. struct dirent *direntp;
  230.  
  231.    int valuemode=0;
  232.    strcpy(namebuf,dir);
  233.    addbackslash(namebuf);
  234.    strcat(namebuf,name);
  235.    if (!strchr(name,'.')) {valuemode=1;strcat(namebuf,".*");}
  236.    buf[0]=0; d_time=d_date=0;
  237.  
  238. dirp = opendir(namebuf); 
  239. if (dirp == NULL) {
  240.    fprintf(stderr,"Could not opendir: %s\r\n",namebuf);
  241. } else {
  242.    for (;;) {
  243.       direntp = readdir(dirp);
  244.       if (direntp==NULL) break;
  245.       p=strchr(direntp->d_name,'.');
  246.       if (p==NULL) continue;
  247.       p++;
  248.       if (!isdigit(*p)) continue;
  249.       if ((direntp->d_date > d_date) && (direntp->d_time > d_time))
  250.            {
  251.            strcpy(buf,direntp->d_name);
  252.            d_date = direntp->d_date;
  253.            d_time = direntp->d_time;
  254.             }
  255.     }
  256.    closedir(dirp);
  257. } /* endif */
  258.  
  259.    if (!buf[0])
  260.      {
  261.       cprintf("No nodelist files found.\r\n");
  262.       exit(1);
  263.      }
  264.    strcpy(namebuf,dir);
  265.    addbackslash(namebuf);
  266.    strcat(namebuf,buf);
  267.    return namebuf;
  268. }
  269.  
  270. void main(int argc, char *argv[])
  271. {
  272. //  clrscr();
  273.   if (argc > 1)
  274.    {
  275.     if (strcmp(argv[1],"/?")==NULL)
  276.       {
  277.   cputs("\r\n\r\n");
  278.   cprintf("Nodelist Index Builder for GIGO vsn "__DATE__"\r\n");
  279.   cprintf("Usage:  COMPILE [path] [nodelistname] [/Append]\r\n"
  280.                  " Note that PATH and NODELISTNAME _must_ be specified\r\n"
  281.                  " seperately for this program to work.  It's quick, dirty,\r\n"
  282.                  " and way fast!  /APPEND may be specified if you need to\r\n"
  283.          " run this for more than one nodelist file.  Sample:\r\n"
  284.                  "   C> COMPILE .\ NODELIST\r\n"
  285.          "   C> COMPILE .\ EGGNET /A\r\n");
  286.      return;
  287.       }
  288.     strcpy(defaultdir,argv[1]);
  289.      }
  290.   addbackslash(defaultdir);
  291.   if (argc > 2)
  292.     strcpy(defaultnodelist,argv[2]);
  293.   if (argc > 3)
  294.     for (int index=3; index < argc; index++)
  295.      {
  296.        if (strnicmp(argv[index],"/A",2)==NULL)
  297.          append=1;
  298.      }
  299.   strcpy(defaultnodelist,getnodelist(defaultdir,defaultnodelist));
  300.   cprintf("Nodelist Index Builder for GIGO vsn "__DATE__"\r\n");
  301.   cprintf("Type %s /? for help info.\r\n",argv[0]);
  302.   cprintf(       "\r\nOptions selected:\r\n"
  303.                  "  Directory:  %s\r\n"
  304.                  "  Nodelist:   %s\r\n"
  305.                  "  Append:     %s\r\n\n",defaultdir,defaultnodelist,append?"Yes":"No (Overwriting)");
  306.  
  307.  if (!append)
  308.   unlink("NODELIST.GIG");
  309.  parsenodelist();
  310.  cprintf("\r\n");
  311. }
  312.